package com.it.whitejotterapi.service.impl;

import com.it.whitejotterapi.service.SendEmail;
import com.it.whitejotterapi.tools.JsonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: 羡羡
 * @Date: 2022/05/29/10:36
 */
@Service
public class SendEmailServiceImpl implements SendEmail {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送邮件
     * @param to 接收人的邮箱
     * @param subject 主题
     * @param content 内容
     * @return
     */
    @Override
    public JsonResult sendmessage(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            //发件人
            message.setFrom(from);
            //收件人
            message.setTo(to);
            //邮件主题
            message.setSubject(subject);
            //邮件内容
            message.setText(content);
            //发送邮件
            javaMailSender.send(message);
            return new JsonResult(200,"发送成功！");
        }catch (Exception e){
            return new JsonResult(500,e.getMessage());
        }
    }
}
